/*     */ package com.cr.common.utils.file;
/*     */ 
/*     */ import com.cr.common.config.RuoYiConfig;
/*     */ import com.cr.common.utils.MyDateUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.uuid.IdUtils;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLEncoder;
/*     */ import java.nio.charset.StandardCharsets;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.io.IOUtils;
/*     */ import org.apache.commons.lang3.ArrayUtils;
/*     */ 
/*     */ public class FileUtils {
/*  28 */   public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
/*     */   
/*     */   public static void writeBytes(String filePath, OutputStream os) throws IOException {
/*  39 */     FileInputStream fis = null;
/*     */     try {
/*  42 */       File file = new File(filePath);
/*  43 */       if (!file.exists())
/*  45 */         throw new FileNotFoundException(filePath); 
/*  47 */       fis = new FileInputStream(file);
/*  48 */       byte[] b = new byte[1024];
/*     */       int length;
/*  50 */       while ((length = fis.read(b)) > 0)
/*  52 */         os.write(b, 0, length); 
/*  55 */     } catch (IOException e) {
/*  57 */       throw e;
/*     */     } finally {
/*  61 */       IOUtils.close(os);
/*  62 */       IOUtils.close(fis);
/*     */     } 
/*     */   }
/*     */   
/*     */   public static String writeImportBytes(byte[] data) throws IOException {
/*  75 */     return writeBytes(data, RuoYiConfig.getImportPath());
/*     */   }
/*     */   
/*     */   public static String writeBytes(byte[] data, String uploadDir) throws IOException {
/*  88 */     FileOutputStream fos = null;
/*  89 */     String pathName = "";
/*     */     try {
/*  92 */       String extension = getFileExtendName(data);
/*  93 */       pathName = MyDateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
/*  94 */       File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
/*  95 */       fos = new FileOutputStream(file);
/*  96 */       fos.write(data);
/*     */     } finally {
/* 100 */       IOUtils.close(fos);
/*     */     } 
/* 102 */     return FileUploadUtils.getPathFileName(uploadDir, pathName);
/*     */   }
/*     */   
/*     */   public static boolean deleteFile(String filePath) {
/* 113 */     boolean flag = false;
/* 114 */     File file = new File(filePath);
/* 116 */     if (file.isFile() && file.exists()) {
/* 118 */       file.delete();
/* 119 */       flag = true;
/*     */     } 
/* 121 */     return flag;
/*     */   }
/*     */   
/*     */   public static boolean isValidFilename(String filename) {
/* 132 */     return filename.matches(FILENAME_PATTERN);
/*     */   }
/*     */   
/*     */   public static boolean checkAllowDownload(String resource) {
/* 144 */     if (MyStringUtils.contains(resource, ".."))
/* 146 */       return false; 
/* 150 */     if (ArrayUtils.contains((Object[])MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
/* 152 */       return true; 
/* 156 */     return false;
/*     */   }
/*     */   
/*     */   public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
/* 168 */     String agent = request.getHeader("USER-AGENT");
/* 169 */     String filename = fileName;
/* 170 */     if (agent.contains("MSIE")) {
/* 173 */       filename = URLEncoder.encode(filename, "utf-8");
/* 174 */       filename = filename.replace("+", " ");
/* 176 */     } else if (agent.contains("Firefox")) {
/* 179 */       filename = new String(fileName.getBytes(), "ISO8859-1");
/* 181 */     } else if (agent.contains("Chrome")) {
/* 184 */       filename = URLEncoder.encode(filename, "utf-8");
/*     */     } else {
/* 189 */       filename = URLEncoder.encode(filename, "utf-8");
/*     */     } 
/* 191 */     return filename;
/*     */   }
/*     */   
/*     */   public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
/* 203 */     String percentEncodedFileName = percentEncode(realFileName);
/* 205 */     StringBuilder contentDispositionValue = new StringBuilder();
/* 206 */     contentDispositionValue.append("attachment; filename=")
/* 207 */       .append(percentEncodedFileName)
/* 208 */       .append(";")
/* 209 */       .append("filename*=")
/* 210 */       .append("utf-8''")
/* 211 */       .append(percentEncodedFileName);
/* 213 */     response.setHeader("Content-disposition", contentDispositionValue.toString());
/*     */   }
/*     */   
/*     */   public static String percentEncode(String s) throws UnsupportedEncodingException {
/* 224 */     String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
/* 225 */     return encode.replaceAll("\\+", "%20");
/*     */   }
/*     */   
/*     */   public static String getFileExtendName(byte[] photoByte) {
/* 236 */     String strFileExtendName = "jpg";
/* 237 */     if (photoByte[0] == 71 && photoByte[1] == 73 && photoByte[2] == 70 && photoByte[3] == 56 && (photoByte[4] == 55 || photoByte[4] == 57) && photoByte[5] == 97) {
/* 240 */       strFileExtendName = "gif";
/* 242 */     } else if (photoByte[6] == 74 && photoByte[7] == 70 && photoByte[8] == 73 && photoByte[9] == 70) {
/* 244 */       strFileExtendName = "jpg";
/* 246 */     } else if (photoByte[0] == 66 && photoByte[1] == 77) {
/* 248 */       strFileExtendName = "bmp";
/* 250 */     } else if (photoByte[1] == 80 && photoByte[2] == 78 && photoByte[3] == 71) {
/* 252 */       strFileExtendName = "png";
/*     */     } 
/* 254 */     return strFileExtendName;
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-common-4.7.0.jar!\com\cr\commo\\utils\file\FileUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */